<?php

namespace App\Http\Controllers\Dashboard;

use App\Http\Requests\Dashboard\UserIndexRequest;
use App\Http\Requests\Dashboard\UserStoreRequest;
use App\Http\Requests\Dashboard\UserUpdateRequest;

use App\Http\Resources\Dashboard\UserResource;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param \App\Models\User $user
     * @param \App\Http\Requests\Dashboard\UserIndexRequest $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function index(UserIndexRequest $request, User $user)
    {
        if ($request->has('name')) {
            $user = $user->like('name', $request->name);
        }
        if ($request->has('email')) {
            $user = $user->like('email', $request->email);
        }
        if ($request->has('roles')) {
            $user = $user->roles($request->roles);
        }
        if ($request->has('trashed')) {
            $user = $user->trashedType($request->trashed);
        }

        if ($request->has('created_at_from')) {
            $user = $user->where(User::CREATED_AT, '>', $request->created_at_from);
        }
        if ($request->has('created_at_to')) {
            $user = $user->where(User::CREATED_AT, '<', $request->created_at_to);
        }

        if ($request->has('order_by')) {
            $user = $user->orderBy($request->order_by, $request->get('order_direction', 'asc'));
        }

        return UserResource::collection($user->paginate($request->get('per_page', 15)));
    }

    public function filters()
    {
        return ['roles' => User::ROLES, 'trashed_types' => User::$trashedTypes];
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \App\Http\Requests\Dashboard\UserStoreRequest $request
     * @return void
     */
    public function store(UserStoreRequest $request)
    {
        $user = new User($request->only(['name', 'role', 'email']));
        $user->password = Hash::make($request->password);
        $user->save();
    }

    /**
     * Display the specified resource.
     *
     * @param \App\Models\User $user
     * @return \App\Http\Resources\Dashboard\UserResource
     */
    public function show(User $user)
    {
        return new UserResource($user);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \App\Models\User $user
     * @param \App\Http\Requests\Dashboard\UserUpdateRequest $request
     * @return void
     */
    public function update(UserUpdateRequest $request, User $user)
    {
        $user->fill($request->only(['name', 'role', 'email']));
        if ($request->has('password')) {
            $user->password = Hash::make($request->password);
        }

        $user->save();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param \App\Models\User $user
     * @return void
     * @throws \Exception
     */
    public function destroy(User $user)
    {
        $user->delete();
    }
}
